﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Preloader for Image instances
**/

package com.alexrindo.flashview.preloader 
{
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class Preloader extends MovieClip
	{	
		private var _percent:int
		private var _animationTimer:Timer
		private var _preloaderPieceArray:Array
		private var _iteration:int
		
		public function Preloader() 
		{
			init()
		}
		
		private function init():void
		{
			_preloaderPieceArray = new Array()
			
			_animationTimer = new Timer(50,0)
			_animationTimer.addEventListener(TimerEvent.TIMER, animateProgress)
			_animationTimer.start()
		}
		
		private function animateProgress(e:TimerEvent):void
		{
			for (var i:int = 1; i < 6; i++) 
			{
				_preloaderPieceArray[i] = "pl" + (((_iteration + i) % 12) + 1)
				this[_preloaderPieceArray[i]].alpha = 0.25 + (i*0.15)
			}
			_iteration++;
		}
		
		public function updatePreloader(progress:int)
		{
			_percent = progress
			percent.text = _percent.toString()
			
			if (progress == 100)
			{
				_animationTimer.stop()
			}
		}
	}
}